/*
 * Copyright (c) 2014 Hai Bison
 *
 * See the file LICENSE at the root directory of this project for copying
 * permission.
 */

package haibison.simpleprovider;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.provider.BaseColumns;

/**
 * Simple contract.
 *
 * @author Hai Bison
 *
 */
public class SimpleContract implements BaseColumns {

    /**
     * This is singleton class.
     */
    protected SimpleContract() {
    }// SimpleContract()

    /**
     * Creation date.
     * <p/>
     * Type: {@link String} representing a {@link Long} (because SQLite will
     * round a long value if we insert it directly).
     */
    public static final String _DATE_CREATED = "_date_created";

    /**
     * Modification date.
     * <p/>
     * Type: {@link String} representing a {@link Long} (because SQLite will
     * round a long value if we insert it directly).
     */
    public static final String _DATE_MODIFIED = "_date_modified";

    /**
     * {@code true} as string.
     */
    public static final String TRUE_AS_STRING = Boolean.TRUE.toString();

    /**
     * {@code false} as string.
     */
    public static final String FALSE_AS_STRING = Boolean.FALSE.toString();

    /**
     * {@code true} as integer.
     */
    public static final int TRUE_AS_INT = 1;

    /**
     * {@code false} as integer.
     */
    public static final int FALSE_AS_INT = 0;

    /**
     * Invalid ID (of column {@link BaseColumns#_ID}).
     */
    public static final long INVALID_ID = -1;

    /**
     * Formats {@code n} to text to store to database. This method prefixes the
     * output string with {@code "0"} to make sure the result will always have
     * same length (for a {@link Long}). So it will work when comparing
     * different values as text.
     * <p/>
     * <b>Note:</b> This doesn't escape the result. If you need to escape SQL,
     * use {@link #formatNumber(long, boolean)}.
     *
     * @param n
     *            a long value.
     * @return the formatted string.
     */
    public static final String formatNumber(long n) {
        return formatNumber(n, false);
    }// formatNumber()

    /**
     * Formats {@code n} to text to store to database. This method prefixes the
     * output string with {@code "0"} to make sure the result will always have
     * same length (for a {@link Long}). So it will work when comparing
     * different values as text.
     *
     * @param n
     *            a long value.
     * @param escapeSql
     *            set to {@code true} if you want to escape SQL for returning
     *            string.
     * @return the formatted string.
     */
    public static final String formatNumber(long n, boolean escapeSql) {
        String s = String.format("%020d", n);
        return escapeSql ? DatabaseUtils.sqlEscapeString(s) : s;
    }// formatNumber()

    /**
     * Gets content type.
     *
     * @param authority
     *            the authority.
     * @return content type.
     */
    public static final String getContentType(String authority) {
        return "vnd.android.cursor.dir/vnd." + authority;
    }// getContentType()

    /**
     * Gets content item type.
     *
     * @param authority
     *            the authority.
     * @return content item type.
     */
    public static final String getContentItemType(String authority) {
        return "vnd.android.cursor.item/vnd." + authority;
    }// getContentItemType()

    /**
     * Gets content URI.
     *
     * @param authority
     *            the authority.
     * @return the content URI.
     */
    public static final Uri getContentUri(String authority) {
        return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
                .authority(authority).build();
    }// getContentUri()

    /**
     * Gets content item base URI. You can append to this URI the item ID to
     * retrieve its content.
     *
     * @param authority
     *            the authority.
     * @return the content item base URI.
     */
    public static final Uri getContentItemBaseUri(String authority) {
        return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
                .authority(authority).build();
    }// getContentItemBaseUri()

    /**
     * Gets content item URI.
     *
     * @param authority
     *            the authority.
     * @param id
     *            the ID.
     * @return the content item URI.
     */
    public static final Uri getContentItemUri(String authority, long id) {
        return ContentUris.withAppendedId(getContentItemBaseUri(authority), id);
    }// getContentItemUri()

}
